<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\FeishuMini\ProgrammingEnum;
use App\Models\Common\CityResidencePriceModel;
use App\Models\Common\MapNationalBrandShopModel;
use App\Models\Common\MapNationalCommunityModel;
use App\Models\Common\MapNationalIndustrialParkModel;
use App\Models\Common\MapNationalStreetModel;
use App\Models\Common\MapNationalUniversitiesModel;
use App\Models\Common\MapTownshipPlanAreaModel;
use App\Models\OpenShop\Common\SiteLocationMallModel;
use App\Service\OpenShop\SiteLocation\GuanyuanDataService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class WgAreaAddGeo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wanggui:area-add-geo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '网规经纬度缓存';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    const GEO_WG_KEY              = "tiger:wanggui:dot";
    const GEO_WG_TYPE_SHOP_PREFIX = "KFZC";

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //先删掉数据再同步
        Redis::del('tiger:wanggui:dot');
        ini_set('memory_limit', '1024M');
        $num = 1;
        CityResidencePriceModel::query()
                               ->chunk(1000, function ($arr) use (&$num) {
                                   foreach ($arr as $value) {
                                       Redis::geoAdd('tiger:wanggui:dot', $value->centerLongitude,
                                           $value->centerLatitude,
                                           'XQ99' . str_pad($value->id, 7, '0', STR_PAD_LEFT));
                                   }
                                   $this->info(1000 * $num);
                                   $num++;
                               });

        MapNationalIndustrialParkModel::query()
                                      ->chunk(1000, function ($arr) use (&$num) {
                                          foreach ($arr as $value) {
                                              Redis::geoAdd('tiger:wanggui:dot', $value->lon, $value->lat,
                                                  $value->wgNumber);
                                          }
                                          $this->info(1000 * $num);
                                          $num++;
                                      });

        SiteLocationMallModel::query()
                             ->where('grade', '>', 0)
                             ->chunk(1000, function ($arr) use (&$num) {
                                 foreach ($arr as $value) {
                                     Redis::geoAdd('tiger:wanggui:dot', $value->lng, $value->lat, $value->wgNumber);
                                 }
                                 $this->info(1000 * $num);
                                 $num++;
                             });

        MapTownshipPlanAreaModel::query()
                                ->chunk(1000, function ($arr) use (&$num) {
                                    foreach ($arr as $value) {
                                        Redis::geoAdd('tiger:wanggui:dot', $value->lng, $value->lat, $value->wgNumber);
                                    }
                                    $this->info(1000 * $num);
                                    $num++;
                                });

        MapNationalUniversitiesModel::query()
                                    ->chunk(1000, function ($arr) use (&$num) {
                                        foreach ($arr as $value) {
                                            Redis::geoAdd('tiger:wanggui:dot', $value->lon, $value->lat,
                                                $value->wgNumber);
                                        }
                                        $this->info(1000 * $num);
                                        $num++;
                                    });

        MapNationalStreetModel::query()
                              ->chunk(1000, function ($arr) use (&$num) {
                                  foreach ($arr as $value) {
                                      Redis::geoAdd('tiger:wanggui:dot', $value->lng, $value->lat, $value->wgNumber);
                                  }
                                  $this->info(1000 * $num);
                                  $num++;
                              });

        MapNationalCommunityModel::query()
                              ->chunk(1000, function ($arr) use (&$num) {
                                  foreach ($arr as $value) {
                                      Redis::geoAdd('tiger:wanggui:dot', $value->lng, $value->lat, $value->wgNumber);
                                  }
                                  $this->info(1000 * $num);
                                  $num++;
                              });


        MapNationalBrandShopModel::query()
                                 ->chunk(1000, function ($arr) use (&$num) {
                                     foreach ($arr as $value) {
                                         switch ($value->brand) {
                                             case '正新鸡排':
                                                 $prefix = 'ZXJP';
                                                 break;
                                             case '华莱士':
                                                 $prefix = 'HLS';
                                                 break;
                                             case '阿甘锅盔':
                                                 $prefix = 'AGGK';
                                                 break;
                                             case '茶百道':
                                                 $prefix = 'CBD';
                                                 break;
                                             case '古茗':
                                                 $prefix = 'GM';
                                                 break;
                                             case '煌上煌':
                                                 $prefix = 'HSH';
                                                 break;
                                             case '久久丫':
                                                 $prefix = 'JJY';
                                                 break;
                                             case '绝味鸭脖':
                                                 $prefix = 'JWYB';
                                                 break;
                                             case '罗森':
                                                 $prefix = 'LS';
                                                 break;
                                             case '蜜雪冰城':
                                                 $prefix = 'MXBC';
                                                 break;
                                             case '七分甜':
                                                 $prefix = 'QFT';
                                                 break;
                                             case '赛百味':
                                                 $prefix = 'SBW';
                                                 break;
                                             case '书亦烧仙草':
                                                 $prefix = 'SYSXC';
                                                 break;
                                             case '喜姐炸串':
                                                 $prefix = 'XJZC';
                                                 break;
                                             case '幸运咖':
                                                 $prefix = 'XYK';
                                                 break;
                                             case '一只酸奶牛':
                                                 $prefix = 'YZSNN';
                                                 break;
                                             case '悦刻':
                                                 $prefix = 'KY';
                                                 break;
                                             case '周黑鸭':
                                                 $prefix = 'HZY';
                                                 break;
                                             case 'Coco奶茶':
                                                 $prefix = 'CCNC';
                                                 break;
                                             case '星巴克咖啡':
                                                 $prefix = 'XBK';
                                                 break;
                                             case '肯德基':
                                                 $prefix = 'KFC';
                                                 break;
                                             case '奈雪の茶':
                                                 $prefix = 'NXDC';
                                                 break;
                                             case '1点点':
                                                 $prefix = 'YDD';
                                                 break;
                                             case '沪上阿姨':
                                                 $prefix = 'HSAY';
                                                 break;
                                             case '益禾堂':
                                                 $prefix = 'YHT';
                                                 break;
                                             case 'MannerCoffee':
                                                 $prefix = 'MN';
                                                 break;
                                             default:
                                                 $prefix = 'PR';
                                         }
                                         //Redis::geoAdd('tiger:wanggui:dot', $value->lon, $value->lat, $prefix . str_pad($value->id, 6, '0', STR_PAD_LEFT));
                                         Redis::geoAdd('tiger:wanggui:dot', $value->lon, $value->lat, $value->wgNumber);
                                     }
                                     $this->info(1000 * $num);
                                     $num++;
                                 });
        try {
            GuanyuanDataService::setTimeOut(30);
            $kfShop  = GuanyuanDataService::newShopList();
        } catch (\Exception $exception) {
            $kfShop = [];
        }
        foreach ($kfShop as $shop) {
            if (!$shop['门店经度'] || !$shop['门店纬度']) {
                continue;
            }
            Redis::geoAdd('tiger:wanggui:dot', $shop['门店经度'], $shop['门店纬度'], 'KFZC' . (ProgrammingEnum::KF_SHOP_STATUS_REVERSE_MAPPING[$shop['状态']] ?? 0) . $shop['铺位号']);
        }
        $this->info('OK');
    }
}
